var express = require('express');
var router = express.Router();


const app = express();
// 引入 path 模块
const path = require('path');
// 处理post请求的参数, 将来在路由中可以直接通过req.body获取前端通过post提交的数据
app.use(express.urlencoded({ extended: false }));
// 引入 fs 进行文件读写
const fs = require('fs');
const multer = require('multer');
const upload = multer({ dest: './public/uploads' });
router.post('/', upload.any(), function (req, res, next) {
    const files = req.files[0];
    // 创建可读流
    const reads = fs.createReadStream(files.path);
    // 创建可写流
    const writes = fs.createWriteStream('.public/uploads' + files.originalname);
    // 管道连接
    reads.pipe(writes);
    res.send({img:files.path,code: 2000, msg: 'ok'});
});
module.exports = router;